package com.bdzl.biz.controller;

import com.bdzl.biz.service.*;
import com.bdzl.biz.servicefeign.*;
import com.bdzl.pub.controller.BaseController;
import com.bdzl.pub.entity.data.UserInfoDO;
import com.bdzl.pub.entity.flow.ActChkRecDO;
import com.bdzl.pub.entity.flow.ActNodeNextDO;
import com.bdzl.pub.entity.flow.ActOdrStatusDO;
import com.bdzl.pub.entity.loan.*;
import com.bdzl.pub.entity.sys.UserDO;
import com.bdzl.pub.time.DateUtils;
import com.bdzl.pub.string.StringUtils;
import com.bdzl.pub.util.file.BASE64DecodedMultipartFile;
import com.bdzl.pub.util.file.OssUploadFileUtil;
import com.bdzl.pub.util.param.PageUtils;
import com.bdzl.pub.util.param.Query;
import com.bdzl.pub.util.param.R;
import com.bdzl.pub.vo.biz.CreditBigDataVO;
import com.bdzl.pub.vo.biz.PreliminaryReviewVO;
import com.bdzl.pub.vo.data.UserInfoVO;
import com.bdzl.pub.vo.flow.ActNodeVO;
import com.bdzl.pub.vo.loan.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.*;

/**
 * 融资租赁订单表
 *
 * @author yangyicai
 * @email yangyicai@foxmail.com
 * @date 2021-05-11 15:23:42
 */

@Controller
@RequestMapping("/bdzl/order")
public class OrderController extends BaseController {
	@Autowired
	private OrderService orderService;
	@Autowired
	private AdminFeignService adminFeignService;
	@Autowired
	private FlowFeignService flowFeignService;
	@Autowired
	private CreditFeignService creditFeignService;
	@Autowired
	private AuthFeignService authFeignService;
	@Autowired
	private OrderCallService orderCallService;
	@Autowired
	private OrderVisitorService orderVisitorService;
	@Autowired
	private OrderRelService orderRelService;
	@Autowired
	private OrderVehService orderVehService;
	@Autowired
	private OrderPayAccountService orderPayAccountService;
	@Autowired
	private OrderAccountService orderAccountService;
	@Autowired
	private OrderCustService orderCustService;
	@Autowired
	private OrderVisitImgService orderVisitImgService;
	@Autowired
	private EContractFeignService eContractFeignService;
	@Autowired
	private OdrFileInfoService odrFileInfoService;
    @Autowired
    private OrderPayRecService orderPayRecService;

	@GetMapping()
	String Order(){
	    return "bdzl/order/order";
	}

	@ResponseBody
	@GetMapping("/list")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<OrderDO> orderList = orderService.list(query);
		int total = orderService.count(query);
		PageUtils pageUtils = new PageUtils(orderList, total);
		return pageUtils;
	}

	@GetMapping("/add")
	String add(){
	    return "bdzl/order/add";
	}

	@GetMapping("/edit/{odrNo}")
	String edit(@PathVariable("odrNo") String odrNo,Model model){
		OrderDO order = orderService.get(odrNo);
		model.addAttribute("order", order);
	    return "bdzl/order/edit";
	}

	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	public R save(@RequestBody OrderDO order){
		if(orderService.save(order)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	public R update( OrderDO order){
		orderService.update(order);
		return R.ok();
	}

	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	public R remove( String odrNo){
		if(orderService.remove(odrNo)>0){
		return R.ok();
		}
		return R.error();
	}

	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	public R remove(@RequestParam("ids[]") String[] odrNos){
		orderService.batchRemove(odrNos);
		return R.ok();
	}
	/**
	* @Description: TODO 进行校验，并报件
	* @param userInfoVOs
	* @return: com.bdzl.pub.util.param.R
	* @Author: Ming
	* @Date: 2021/5/17 11:04
	*/
	@ResponseBody
	@PostMapping("/newspaper")
	public R newspaper(@RequestBody @Valid UserInfoVO[] userInfoVOs,HttpServletRequest request){
		//获取当前登录用户数据
		UserDO userInfo = orderService.getUserInfo(request);
		return orderService.newspaper(userInfoVOs,userInfo.getUserId());
	}
	/**
	 * @Description: TODO 我的报件
	 * @param params
	 * @return: com.bdzl.pub.util.param.R
	 * @Author: Ming
	 * @Date: 2021/5/17 11:04
	 */
	@ResponseBody
	@PostMapping("/newsletterList")
	public R newsletterList(@RequestBody Map<String, Object> params,HttpServletRequest request){
		//获取当前登录者id
		UserDO userInfo = orderService.getUserInfo(request);
		params.put("odrCreator",userInfo.getUserId());
		Query query = new Query(params);
		//根据用户id查询用户角色
		List<String> roleIds = orderService.getRoleByUserId(userInfo.getUserId());
		List<OrderVO> orderVOList = orderService.newsletterList(query);
		//根据角色id查询所拥有的所有可以操作的状态值
		List<String> statValByRole = orderService.getStatValByRole(roleIds);
		if (orderVOList.size()>0){
			for (OrderVO orderVO : orderVOList) {

				StringBuilder reportStatus =new StringBuilder();
				//用，分割字符串转换为数组
				List<String> valList = new ArrayList<>(Arrays.asList(orderVO.getOdrState().split(",")));
				//当前登录者为业务员、或录单员
				if (roleIds.contains("3")||roleIds.contains("9")){
					//判断有没有，
					boolean contains = orderVO.getOdrState().contains(",");
					//如果包含，则是并行状态
					if (contains){
						for (String s : statValByRole) {
							//如果查询出来的订单状态包含了该用户操作的状态，修改为该用户操作的状态
							if (valList.contains(s)){
								orderVO.setOdrState(s);
							}
						}
					}
					//查询当前订单状态
					List<ActNodeVO> odrStatusByStatVal = flowFeignService.getOdrStatusByStatVal(orderVO.getOdrState());
					if (odrStatusByStatVal != null && odrStatusByStatVal.size()>0){
						orderVO.setReportStatus(odrStatusByStatVal.get(0).getStatName());
					}else {
						orderVO.setReportStatus("");
					}
				}else {
					if (valList!=null && valList.size()>0){
						for (String val : valList) {
							List<ActNodeVO> odrStatusByStatVal = flowFeignService.getOdrStatusByStatVal(val);
							if (odrStatusByStatVal.size()>0){
								reportStatus.append(odrStatusByStatVal.get(0).getStatName());
							}
						}
						orderVO.setReportStatus(String.valueOf(reportStatus));
					}
				}
			}
		}
		PageUtils pageUtils = new PageUtils(orderVOList, orderVOList.size());
		return R.ok(pageUtils);
	}
	/**
	 * @Description: TODO 待处理审核列表
	 * @param params
	 * @return: com.bdzl.pub.util.param.R
	 * @Author: Ming
	 * @Date: 2021/5/17 11:04
	 */
	@ResponseBody
	@PostMapping("/checkList")
	public R checkList(@RequestBody Map<String, Object> params,HttpServletRequest request){
		//获取当前登录用户数据
		UserDO userInfo = orderService.getUserInfo(request);
		//根据用户id查询用户角色
		List<String> roleIds = orderService.getRoleByUserId(userInfo.getUserId());
		params.put("list",roleIds);
		//根据角色id查询所拥有的所有可以操作的状态值
		List<String> statValByRole = orderService.getStatValByRole(roleIds);
		Query query = new Query(params);
		List<OrderVO> orderVOList = orderService.checkList(query);
		if (orderVOList.size()>0){
			for (OrderVO orderVO : orderVOList) {

				StringBuilder reportStatus =new StringBuilder();
				//用，分割字符串转换为数组
				List<String> valList = new ArrayList<>(Arrays.asList(orderVO.getOdrState().split(",")));
				//当前登录者为业务员、或录单员
				if (roleIds.contains("3")||roleIds.contains("9")){
					//判断有没有，
					boolean contains = orderVO.getOdrState().contains(",");
					//如果包含，则是并行状态
					if (contains){
						for (String s : statValByRole) {
							//如果查询出来的订单状态包含了该用户操作的状态，修改为该用户操作的状态
							if (valList.contains(s)){
								orderVO.setOdrState(s);
							}
						}
					}
					//查询当前订单状态
					List<ActNodeVO> odrStatusByStatVal = flowFeignService.getOdrStatusByStatVal(orderVO.getOdrState());
					if (odrStatusByStatVal != null && odrStatusByStatVal.size()>0){
						orderVO.setReportStatus(odrStatusByStatVal.get(0).getStatName());
					}else {
						orderVO.setReportStatus("");
					}
				}else {
					if (valList!=null && valList.size()>0){
						for (String val : valList) {
							List<ActNodeVO> odrStatusByStatVal = flowFeignService.getOdrStatusByStatVal(val);
							if (odrStatusByStatVal.size()>0){
								reportStatus.append(odrStatusByStatVal.get(0).getStatName());
							}
						}
						orderVO.setReportStatus(String.valueOf(reportStatus));
					}
				}
			}
		}
		PageUtils pageUtils = new PageUtils(orderVOList, orderVOList.size());
		return R.ok(pageUtils);
	}

	/**
	 * @Description: TODO 已处理列表
	 * @param params
	 * @return: com.bdzl.pub.util.param.R
	 * @Author: Ming
	 * @Date: 2021/5/17 11:04
	 */
	@ResponseBody
	@PostMapping("/processedList")
	public R processedList(@RequestBody Map<String, Object> params,HttpServletRequest request){
		//获取当前登录用户数据
		UserDO userInfo = orderService.getUserInfo(request);
		params.put("acrOperator",userInfo.getUserId());
		Query query = new Query(params);
		List<OrderVO> orderVOList = orderService.processedList(query);
		if (orderVOList.size()>0){
			for (OrderVO orderVO : orderVOList) {
				orderVO.setReportStatus("已处理");
			}
		}
		PageUtils pageUtils = new PageUtils(orderVOList, orderVOList.size());
		return R.ok(pageUtils);
	}
	/**
	* @Description: TODO 首页工作台
	* @return: com.bdzl.pub.util.param.R
	* @Author: Ming
	* @Date: 2021/5/27 11:04
	*/
	@ResponseBody
	@GetMapping("/workbench")
	public R workbench(HttpServletRequest request){
		//获取当前登录用户数据
		Map map = new HashMap();
		Map returnMap = new HashMap();
		UserDO userInfo = orderService.getUserInfo(request);
		map.put("acrOperator",userInfo.getUserId());
		map.put("nodeUserVal",userInfo.getUserId());
		//已处理
		List<OrderVO> processedList = orderService.processedList(map);
		//待处理
		List<OrderVO> pendingList = orderService.checkList(map);
		//正常待处理
		Integer pendingY = 0;
		//驳回待处理
		Integer pendingN = 0;

		if (pendingList.size()>0){
			for (OrderVO orderVO : pendingList) {
				if (orderVO.getOdrState().startsWith("Y")){
					pendingY++;
				}
				if (orderVO.getOdrState().startsWith("N")){
					pendingN++;
				}
			}
		}

		returnMap.put("pendingY",pendingY);
		returnMap.put("pendingN",pendingN);
		//待处理总数
		returnMap.put("pending",pendingList.size());
		//已处理总数
		returnMap.put("processed",processedList.size());
		returnMap.put("total",pendingList.size()+processedList.size());
		return R.ok(returnMap);
	}
	/**
	* @Description: TODO 初审
	* @param /odrNo 订单号
	* @return: com.bdzl.pub.util.param.R
	* @Author: Ming
	* @Date: 2021/5/17 17:41
	*/
	@PostMapping("preliminaryReview")
	@ResponseBody
	public R preliminaryReview(@RequestBody Map<String, Object> params ){
		String odrNo = (String) params.get("odrNo");
		PreliminaryReviewVO preliminaryReviewVO = new PreliminaryReviewVO();
		OrderDO orderDO = orderService.get(odrNo);
		//类拷贝
		if (orderDO==null){
			return R.error("订单号错误！");
		}
		BeanUtils.copyProperties(orderDO, preliminaryReviewVO);
		UserInfoVO userInfo = adminFeignService.getUserInfo(orderDO.getOdrCust(),odrNo);
		//查询客户是否签署了授权书
		ContractOnlineSignrecVO contractByUser = orderService.getContractByUser(odrNo, userInfo.getConCardid());
		if (contractByUser!=null){
			userInfo.setCreditAuthorization(true);
			userInfo.setContractOnlineSignrecVO(contractByUser);
		}else {
			userInfo.setContractOnlineSignrecVO(new ContractOnlineSignrecVO());
		}
		UserInfoVO spouse1 = userInfo.getSpouse();
		if (spouse1!=null){
			//查询客户是否签署了授权书
			ContractOnlineSignrecVO contractByUser1 = orderService.getContractByUser(odrNo, spouse1.getConCardid());
			if (contractByUser1!=null){
				spouse1.setCreditAuthorization(true);
				spouse1.setContractOnlineSignrecVO(contractByUser1);
			}
		}else {
			spouse1.setContractOnlineSignrecVO(new ContractOnlineSignrecVO());
		}
		List<UserInfoVO> guarantorList1 = userInfo.getGuarantorList();
		if (guarantorList1!=null && guarantorList1.size()>0){
			for (UserInfoVO userInfoVO : guarantorList1) {
				//查询客户是否签署了授权书
				ContractOnlineSignrecVO contractByUser1 = orderService.getContractByUser(odrNo, userInfoVO.getConCardid());
				if (contractByUser1!=null){
					userInfoVO.setCreditAuthorization(true);
					userInfoVO.setContractOnlineSignrecVO(contractByUser1);
				}else {
					userInfoVO.setContractOnlineSignrecVO(new ContractOnlineSignrecVO());
				}
			}
		}
		preliminaryReviewVO.setUserInfoVO(userInfo);
		//征信大数据
		List<CreditBigDataVO> creditBigDataVOList = new ArrayList<>();
        //客户征信数据
        CreditBigDataVO creditBigDataVO = new CreditBigDataVO();
		creditBigDataVO.setUserPhone(userInfo.getConMobile());
		creditBigDataVO.setUserCardid(userInfo.getConCardid());
		creditBigDataVO.setUserNickname(userInfo.getConName());
        creditBigDataVO.setInquirer(userInfo.getConName()+"(客户)");
		//查询客户是否签署了授权书
		ContractOnlineSignrecVO contractByUser1 = orderService.getContractByUser(odrNo, userInfo.getConCardid());
		if (contractByUser1!=null){
			creditBigDataVO.setCreditAuthorization(true);
			creditBigDataVO.setContractOnlineSignrecVO(contractByUser1);

		}else {
			creditBigDataVO.setContractOnlineSignrecVO(new ContractOnlineSignrecVO());
		}
        //查询客户征信数据
        CreditVO creditByCardId = creditFeignService.getCreditByCardId(userInfo.getConCardid());
        if (creditByCardId!=null){
            if (creditByCardId.getCreditModtime()!=null){
                creditBigDataVO.setQueryTime(creditByCardId.getCreditModtime());
            }else {
                creditBigDataVO.setQueryTime(creditByCardId.getCreditCretime());
            }

			creditBigDataVO.setCreditReport(true);
            creditBigDataVO.setCreditFilePath(creditByCardId.getCreditFilePath());
			if (creditByCardId.getCreditType()!=null){
				creditBigDataVO.setCreditType(creditByCardId.getCreditType());
			}
        }
		System.out.println("-----------"+creditBigDataVO);
        creditBigDataVOList.add(creditBigDataVO);

		//配偶数据
		UserInfoDO spouse = userInfo.getSpouse();
		if (spouse!=null){
			//配偶征信数据
			CreditBigDataVO spouseCreditBigDataVO = new CreditBigDataVO();
			spouseCreditBigDataVO.setUserPhone(spouse.getConMobile());
			spouseCreditBigDataVO.setUserCardid(spouse.getConCardid());
			spouseCreditBigDataVO.setUserNickname(spouse.getConName());
			spouseCreditBigDataVO.setInquirer(spouse.getConName()+"(配偶)");
			//查询客户是否签署了授权书
			ContractOnlineSignrecVO contractByUser2 = orderService.getContractByUser(odrNo, spouse.getConCardid());
			if (contractByUser2!=null){
				spouseCreditBigDataVO.setCreditAuthorization(true);
				spouseCreditBigDataVO.setContractOnlineSignrecVO(contractByUser2);
			}else {
				spouseCreditBigDataVO.setContractOnlineSignrecVO(new ContractOnlineSignrecVO());
			}
			//查询客户征信数据
			CreditVO spouseCreditByCardId = creditFeignService.getCreditByCardId(spouse.getConCardid());
			if (spouseCreditByCardId!=null){
				if (spouseCreditByCardId.getCreditModtime()!=null){
					spouseCreditBigDataVO.setQueryTime(spouseCreditByCardId.getCreditModtime());
				}else {
					spouseCreditBigDataVO.setQueryTime(spouseCreditByCardId.getCreditCretime());
				}

				spouseCreditBigDataVO.setCreditReport(true);
				spouseCreditBigDataVO.setCreditFilePath(spouseCreditByCardId.getCreditFilePath());
				if (spouseCreditByCardId.getCreditType()!=null){
					spouseCreditBigDataVO.setCreditType(spouseCreditByCardId.getCreditType());
				}
			}
			creditBigDataVOList.add(spouseCreditBigDataVO);
		}

        //担保人征信数据
        List<UserInfoVO> guarantorList = userInfo.getGuarantorList();
		if (guarantorList.size()>0){
			for (UserInfoDO userInfoDO : guarantorList) {
				//担保人征信数据
				CreditBigDataVO guarantorCreditBigDataVO = new CreditBigDataVO();
				guarantorCreditBigDataVO.setUserPhone(userInfoDO.getConMobile());
				guarantorCreditBigDataVO.setUserCardid(userInfoDO.getConCardid());
				guarantorCreditBigDataVO.setUserNickname(userInfoDO.getConName());
				guarantorCreditBigDataVO.setInquirer(userInfoDO.getConName()+"(担保人)");
				//查询客户是否签署了授权书
				ContractOnlineSignrecVO contractByUser2 = orderService.getContractByUser(odrNo, userInfoDO.getConCardid());
				if (contractByUser2!=null){
					guarantorCreditBigDataVO.setCreditAuthorization(true);
					guarantorCreditBigDataVO.setContractOnlineSignrecVO(contractByUser2);
				}else {
					guarantorCreditBigDataVO.setContractOnlineSignrecVO(new ContractOnlineSignrecVO());
				}
				//查询客户征信数据
				CreditVO guarantorCreditByCardId = creditFeignService.getCreditByCardId(userInfoDO.getConCardid());
				if (guarantorCreditByCardId!=null){
					if (guarantorCreditByCardId.getCreditModtime()!=null){
						guarantorCreditBigDataVO.setQueryTime(guarantorCreditByCardId.getCreditModtime());
					}else {
						guarantorCreditBigDataVO.setQueryTime(guarantorCreditByCardId.getCreditCretime());
					}
					guarantorCreditBigDataVO.setCreditReport(true);
					guarantorCreditBigDataVO.setCreditFilePath(guarantorCreditByCardId.getCreditFilePath());
					if (guarantorCreditByCardId.getCreditType()!=null){
						guarantorCreditBigDataVO.setCreditType(guarantorCreditByCardId.getCreditType());
					}
				}
				creditBigDataVOList.add(guarantorCreditBigDataVO);
			}
		}
		preliminaryReviewVO.setCreditBigDataVOList(creditBigDataVOList);
        //放入工作流程数据
//        preliminaryReviewVO.setCheckVO(flowFeignService.getNodeByacrOdr(odrNo));
		return R.ok(preliminaryReviewVO);
	}
	/**
	 * @Description: TODO 签完授权书之后
	 * @param /odrNo 订单号
	 * @return: com.bdzl.pub.util.param.R
	 * @Author: Ming
	 * @Date: 2021/5/26 17:09
	 */
	@ResponseBody
	@PostMapping("/powerOfAttorney")
	@Transactional
	public R powerOfAttorney(@RequestBody Map<String, Object> params){
		String odrNo = (String) params.get("odrNo");
		//获取订单数据
		OrderDO orderDO = orderService.get(odrNo);
		if (orderDO!=null){
			//查询订单签署电子授权书的人数
			Integer integer = eContractFeignService.listByCosOdr(odrNo);
			//该订单下所有的人数
			Integer numberOfPeople = 1;
			UserInfoVO userInfo = adminFeignService.getUserInfo(orderDO.getOdrCust(), odrNo);
			if (userInfo!=null && userInfo.getConSpouse() !=null){
				numberOfPeople = numberOfPeople+1;
				if (userInfo.getGuarantorList().size()>0){
					numberOfPeople = numberOfPeople+userInfo.getGuarantorList().size();
				}
			}
			if (numberOfPeople.equals(integer)){
				HashMap map = new HashMap<>();
				map.put("odrNo",odrNo);
				map.put("statVal","Y001");
				map.put("nowStatVal",orderDO.getOdrState());
				this.check(map);
				return R.ok();
			}
		}
		return R.error();
	}
	/**
	 * @Description: TODO 审核
	 * @param /odrNo 订单号
	 * @param /acrChkRes 审核结果描述，例如：初审通过、初审不通过、家访驳回等(不用)
	 * @param /acrChkDescribe 审核附言描述
	 * @param /statVal 订单状态的值
	 * @param /type Y：通过 N：不通过 B:驳回(不用)
	 * @param /annFormula 进行下一节点条件公式 true ,false(不用)
	 * @param /nextNode 下一节点编号(不用)
	 * @param /homeVisit 家访员id
	 * @param /level 客户评级
	 * @param /nowStatVal 当前订单状态的值
	 * @return: com.bdzl.pub.util.param.R
	 * @Author: Ming
	 * @Date: 2021/5/17 17:41
	 */
	@PostMapping("check")
	@ResponseBody
	@Transactional
	public R check(@RequestBody Map<String, Object> params){
		//根据订单号查询当前审核流程
		//订单号
		String odrNo = (String) params.get("odrNo");
//		String acrChkRes = (String) params.get("acrChkRes");
		//审核附言描述
		String acrChkDescribe = (String) params.get("acrChkDescribe");
//		String type = (String) params.get("type");
//		String annFormula = (String) params.get("annFormula");
		//订单状态的值
		String statVal = (String) params.get("statVal");
		//家访员id
		Integer homeVisit = (Integer) params.get("homeVisit");
		//客户评级
		String level = (String) params.get("level");
		//当前的订单状态的值
		String nowStatVal = (String) params.get("nowStatVal");
//		Integer nextNode = (Integer) params.get("nextNode");
		//获取当前订单信息
		OrderDO orderDO = orderService.get(odrNo);
		//用，分割字符串转换为数组
		if (nowStatVal!=null){
			List<String> nowValList = new ArrayList<>(Arrays.asList(nowStatVal.split(",")));
			if (nowValList.size()>0){
				for (String nowVal : nowValList) {
					//根据订单当前状态获取当前节点的信息
					List<ActNodeVO> nowActNodeVOList = flowFeignService.getOdrStatusByStatVal(nowVal);

					//用，分割字符串转换为数组
					List<String> valList = new ArrayList<>(Arrays.asList(statVal.split(",")));
					if (valList.size()>0){
						for (String val : valList) {
							//根据订单状态查询下一步审核节点数据
							List<ActNodeVO> actNodeVOList = flowFeignService.getOdrStatusByStatVal(val);
							ActNodeVO actNodeVO = new ActNodeVO();
							if (actNodeVOList.size()>0){
								actNodeVO = actNodeVOList.get(0);
							}else {
								return R.error("订单状态错误，请联系管理员！");
							}
							//查出来所有的当前订单的审核记录

							if ("parallel".equals(actNodeVO.getStatType())){
								if (nowActNodeVOList.size()>0){
									for (ActNodeVO nowActNodeVO : nowActNodeVOList) {
										//添加中间状态审核记录表数据   并更新当前节点的离开时间
										ActChkRecDO chkRecDO = new ActChkRecDO();
										chkRecDO.setAcrOdr(odrNo);
										chkRecDO.setAcrNodeId(actNodeVO.getNodeId());
										chkRecDO.setAcrCurrentNode(actNodeVO.getNodeTitle());
										chkRecDO.setAcrOperator(Integer.valueOf(actNodeVO.getNodeUserVal()));
										chkRecDO.setAcrBeginTime(new Date());
										if ("Y".equals(actNodeVO.getNodeCloseFlag())) {
											chkRecDO.setAcrChkFlag("Y");
										} else {
											chkRecDO.setAcrChkFlag("N");
										}
										flowFeignService.saveActChkRec(chkRecDO);
										//获取当前节点
										List<ActChkRecDO> actChkRecDOList = flowFeignService.getByOdrNo(odrNo,nowActNodeVO.getNodeId());
										for (ActChkRecDO actChkRecDO : actChkRecDOList) {
											actChkRecDO.setAcrChkRes(actNodeVO.getStatName());
											//更新审核记录表
											actChkRecDO.setAcrEndTime(new Date());
											if (acrChkDescribe!=null){
												actChkRecDO.setAcrChkDescribe(acrChkDescribe);
											}
											//计算处理时间秒为单位
											long l = actChkRecDO.getAcrEndTime().getTime() - actChkRecDO.getAcrBeginTime().getTime();
											actChkRecDO.setAcrUsingTime(Math.toIntExact(l/1000));
											//更新审核记录表
											flowFeignService.update(actChkRecDO);
										}

										//查询订单当前节点所属的订单状态
										orderDO.setOdrState(orderDO.getOdrState().replace(nowVal, val));
										orderService.update(orderDO);
									}
								}
								return R.ok();
							}
							if ("merge".equals(actNodeVO.getStatType())){//下一步节点是合并节点
								if (nowActNodeVOList.size()>0){
									for (ActNodeVO nowActNodeVO : nowActNodeVOList) {
										//根据当前节点id和下一步节点id获取中间值
										ActNodeNextDO byNowAndNext = orderService.getByNowAndNext(nowActNodeVO.getNodeId(), actNodeVO.getNodeId());
										//当所有的审核记录符合合并节点条件 actNodeVO.getAnnType()
										if (orderDO.getOdrState().contains("Y013-1")||orderDO.getOdrState().contains("Y013-2")){
											//添加审核记录表数据
											ActChkRecDO chkRecDO = new ActChkRecDO();
											chkRecDO.setAcrOdr(odrNo);
											chkRecDO.setAcrNodeId(actNodeVO.getNodeId());
											chkRecDO.setAcrCurrentNode(actNodeVO.getNodeTitle());
											chkRecDO.setAcrOperator(Integer.valueOf(actNodeVO.getNodeUserVal()));
											chkRecDO.setAcrBeginTime(new Date());
											if ("Y".equals(actNodeVO.getNodeCloseFlag())) {
												chkRecDO.setAcrChkFlag("Y");
											} else {
												chkRecDO.setAcrChkFlag("N");
											}
											flowFeignService.saveActChkRec(chkRecDO);
											//获取当前节点
											List<ActChkRecDO> actChkRecDOList = flowFeignService.getByOdrNo(odrNo,nowActNodeVO.getNodeId());
											for (ActChkRecDO actChkRecDO : actChkRecDOList) {
												actChkRecDO.setAcrChkRes(actNodeVO.getStatName());
												//更新审核记录表
												actChkRecDO.setAcrEndTime(new Date());
												if (acrChkDescribe!=null){
													actChkRecDO.setAcrChkDescribe(acrChkDescribe);
												}
												//计算处理时间秒为单位
												long l = actChkRecDO.getAcrEndTime().getTime() - actChkRecDO.getAcrBeginTime().getTime();
												actChkRecDO.setAcrUsingTime(Math.toIntExact(l/1000));
												//更新审核记录表
												flowFeignService.update(actChkRecDO);
											}
											//修改订单当前节点所属的订单状态
											orderDO.setOdrState(statVal);
											orderService.update(orderDO);
										}else {//不符合合并条件
											//添加中间状态审核记录表数据   并更新当前节点的离开时间
										/*ActChkRecDO chkRecDO = new ActChkRecDO();
										chkRecDO.setAcrOdr(odrNo);
										chkRecDO.setAcrCurrentNode(actNodeVO.getNodeTitle());
										chkRecDO.setAcrOperator(Integer.valueOf(actNodeVO.getNodeUserVal()));
										chkRecDO.setAcrBeginTime(new Date());
										chkRecDO.setAcrChkFlag("N");
										flowFeignService.saveActChkRec(chkRecDO);*/
											//获取当前节点
											List<ActChkRecDO> actChkRecDOList = flowFeignService.getByOdrNo(odrNo,nowActNodeVO.getNodeId());
											for (ActChkRecDO actChkRecDO : actChkRecDOList) {
												actChkRecDO.setAcrChkRes(actNodeVO.getStatName());
												//更新审核记录表
												actChkRecDO.setAcrEndTime(new Date());
												if (acrChkDescribe!=null){
													actChkRecDO.setAcrChkDescribe(acrChkDescribe);
												}
												//计算处理时间秒为单位
												long l = actChkRecDO.getAcrEndTime().getTime() - actChkRecDO.getAcrBeginTime().getTime();
												actChkRecDO.setAcrUsingTime(Math.toIntExact(l/1000));
												//更新审核记录表
												flowFeignService.update(actChkRecDO);
											}

											//查询订单当前节点所属的订单状态
//							orderDO.getOdrState().replace(nowVal, "Y013-2");

											orderDO.setOdrState(orderDO.getOdrState().replace(nowVal, byNowAndNext.getAnnType()));
											orderService.update(orderDO);
										}
									}
								}
//					return R.ok();
							}else {//如果下一节点不是合并节点，正常走流程
								//获取当前节点
								if (nowActNodeVOList.size()>0) {
									for (ActNodeVO nowActNodeVO : nowActNodeVOList) {
										List<ActChkRecDO> actChkRecDOList = flowFeignService.getByOdrNo(odrNo,nowActNodeVO.getNodeId());

										for (ActChkRecDO actChkRecDO : actChkRecDOList) {
											actChkRecDO.setAcrChkRes(actNodeVO.getStatName());
											//更新审核记录表
											actChkRecDO.setAcrEndTime(new Date());
											if (acrChkDescribe!=null){
												actChkRecDO.setAcrChkDescribe(acrChkDescribe);
											}
											//计算处理时间秒为单位
											long l = actChkRecDO.getAcrEndTime().getTime() - actChkRecDO.getAcrBeginTime().getTime();
											actChkRecDO.setAcrUsingTime(Math.toIntExact(l/1000));
											//更新审核记录表
											flowFeignService.update(actChkRecDO);
										}
									}
								}
								//添加审核记录表数据
								ActChkRecDO chkRecDO = new ActChkRecDO();
								chkRecDO.setAcrOdr(odrNo);
								chkRecDO.setAcrNodeId(actNodeVO.getNodeId());
								chkRecDO.setAcrCurrentNode(actNodeVO.getNodeTitle());
								chkRecDO.setAcrOperator(Integer.valueOf(actNodeVO.getNodeUserVal()));
								chkRecDO.setAcrBeginTime(new Date());
								if ("Y".equals(actNodeVO.getNodeCloseFlag())) {
									chkRecDO.setAcrChkFlag("Y");
								} else {
									chkRecDO.setAcrChkFlag("N");
								}
								flowFeignService.saveActChkRec(chkRecDO);

								//如果需要家访，添加家访表数据
								if ("Y005".equals(statVal)){
									OrderVisitorDO orderVisitorDO = new OrderVisitorDO();
									orderVisitorDO.setVisitorOdr(odrNo);
									orderVisitorDO.setVisitorType("家访员");
									orderVisitorDO.setVisitorUser(homeVisit);
									orderVisitorDO.setVisitorCretime(new Date());
									orderVisitorService.save(orderVisitorDO);
								}
								//更新客户等级
								OrderDO aDo = orderService.get(odrNo);
								if (aDo!=null && level!=null){
									Integer odrCust = aDo.getOdrCust();
									orderCustService.updateByCustCardId(odrNo,odrCust,level);
								}
								//修改订单当前节点所属的订单状态
								orderDO.setOdrState(statVal);
								orderService.update(orderDO);
//					return R.ok();
							}

						}
					}
				}
			}
		}else {
			return R.error();
		}
		/*if ("Y".equals(type)) {
			actChkRecDO.setAcrOperator(super.getUserId());
			actChkRecDO.setAcrChkRes(acrChkRes);
			actChkRecDO.setAcrEndTime(new Date());
			actChkRecDO.setAcrChkDescribe(acrChkDescribe);
			//计算处理时间秒为单位
			actChkRecDO.setAcrUsingTime((Integer) TimeUtils.getDistanceTimes(actChkRecDO.getAcrBeginTime(), actChkRecDO.getAcrEndTime()).get("sec"));
			//更新审核记录表
			flowFeignService.update(actChkRecDO);
			//获取并添加下一节点数据到审核记录表
			if (nextNode == null) {//如果没有传下一节点id，自己获取
				ActNodeVO actNodeVO = flowFeignService.getNextNodeByNow(actChkRecDO.getAcrNodeId(), annFormula);
				ActChkRecDO chkRecDO = new ActChkRecDO();
				chkRecDO.setAcrOdr(odrNo);
				chkRecDO.setAcrNodeId(actNodeVO.getNodeId());
				chkRecDO.setAcrCurrentNode(actNodeVO.getNodeTitle());
				chkRecDO.setAcrOperator(super.getUserId());
				chkRecDO.setAcrBeginTime(new Date());
				if ("Y".equals(actNodeVO.getNodeCloseFlag())) {
					chkRecDO.setAcrChkFlag("Y");
				} else {
					chkRecDO.setAcrChkFlag("N");
				}
				flowFeignService.saveActChkRec(chkRecDO);
			}else {
				ActNodeVO actNodeVO = flowFeignService.getByNodeId(nextNode);
				ActChkRecDO chkRecDO = new ActChkRecDO();
				chkRecDO.setAcrOdr(odrNo);
				chkRecDO.setAcrNodeId(actNodeVO.getNodeId());
				chkRecDO.setAcrCurrentNode(actNodeVO.getNodeTitle());
				chkRecDO.setAcrOperator(super.getUserId());
				chkRecDO.setAcrBeginTime(new Date());
				if ("Y".equals(actNodeVO.getNodeCloseFlag())) {
					chkRecDO.setAcrChkFlag("Y");
				} else {
					chkRecDO.setAcrChkFlag("N");
				}
				flowFeignService.saveActChkRec(chkRecDO);
			}
		}else {*/
		//如果需要家访，添加家访表数据
		/*if ("Y005".equals(statVal)){
			OrderVisitorDO orderVisitorDO = new OrderVisitorDO();
			orderVisitorDO.setVisitorOdr(odrNo);
			orderVisitorDO.setVisitorType("家访员");
			orderVisitorDO.setVisitorUser(homeVisit);
			orderVisitorDO.setVisitorCretime(new Date());
			orderVisitorService.save(orderVisitorDO);
		}
		//更新客户等级
		OrderDO aDo = orderService.get(odrNo);
		if (aDo!=null && level!=null){
			Integer odrCust = aDo.getOdrCust();
			orderCustService.updateByCustCardId(odrNo,odrCust,level);
		}

		//更新订单状态信息
		OrderDO orderDO = orderService.get(odrNo);
		//查询订单当前节点所属的订单状态
		orderDO.setOdrState(statVal);
		orderService.update(orderDO);*/
//		}
		return R.ok();
	}


	/**
	 * @Description: TODO 补充资料
	 * @param /odrNo 订单号
	 * @return: com.bdzl.pub.util.param.R
	 * @Author: Ming
	 * @Date: 2021/5/19 11:04
	 */
	@ResponseBody
	@PostMapping("/additionalMaterials")
	public R additionalMaterials(@RequestBody Map<String, Object> params){
		String odrNo = (String) params.get("odrNo");
		return R.ok(orderService.additionalMaterials(odrNo));
	}
	/**
	 * @Description: TODO 补充资料保存
	 * @param orderVO 订单数据
	 * @return: com.bdzl.pub.util.param.R
	 * @Author: Ming
	 * @Date: 2021/5/19 11:04
	 */
	@ResponseBody
	@PostMapping("/saveAdditionalMaterials")
	@Transactional
	public R saveAdditionalMaterials(@RequestBody OrderVO orderVO, HttpServletRequest request){
		//获取当前登录用户数据
		UserDO userInfo = orderService.getUserInfo(request);
		//获取车辆信息
		OrderVehDO orderVehDO = orderVO.getOrderVehDO();
		if (orderVehDO.getVehId()!=null){
			orderVehDO.setVehModuser(userInfo.getUserId());
			orderVehDO.setVehModtime(new Date());
			orderVehService.update(orderVehDO);
		}else {
			orderVehDO.setVehOdr(orderVO.getOdrNo());
			orderVehDO.setVehCreator(userInfo.getUserId());
			orderVehDO.setVehCretime(new Date());
			orderVehService.save(orderVehDO);
		}
		//获取账户信息
		List<OrderPayAccountDO> orderPayAccountDOList = orderVO.getOrderPayAccountDOList();
		for (OrderPayAccountDO orderPayAccountDO : orderPayAccountDOList) {
			if (orderPayAccountDO.getPayId()!=null){
				orderPayAccountService.update(orderPayAccountDO);
			}else {
				orderPayAccountService.save(orderPayAccountDO);
				Integer payId = orderPayAccountDO.getPayId();
				OrderAccountDO orderAccountDO = new OrderAccountDO();
				orderAccountDO.setPayId(payId);
				orderAccountDO.setOdrNo(orderVO.getOdrNo());
				orderAccountService.save(orderAccountDO);
			}
		}
		orderVO.setOdrApplyTime(new Date());
		orderVO.setOdrModuser(userInfo.getUserId());
		orderVO.setOdrModtime(new Date());
		orderService.update(orderVO);
		//待复审
		OrderDO orderDO = orderService.get(orderVO.getOdrNo());
		HashMap map= new HashMap();
		map.put("odrNo",orderVO.getOdrNo());
		map.put("acrChkDescribe","已补充资料");
		map.put("statVal","Y004");
		map.put("nowStatVal",orderDO.getOdrState());
		this.check(map);
		return R.ok();
	}
	/**
	 * @Description: TODO 根据身份证号查询申请人信息
	 * @param /conCardId 身份证号
	 * @return: com.bdzl.pub.util.param.R
	 * @Author: Ming
	 * @Date: 2021/5/19 20:23
	 */
	@ResponseBody
	@PostMapping("/applicant")
	public R applicant(@RequestBody Map<String, Object> params){
		String conCardId = (String) params.get("conCardId");
		return R.ok(orderService.applicant(conCardId));
	}

	/**
	 * @Description: TODO 保存申请人信息
	 * @param userInfoVO 申请人信息
	 * @return: com.bdzl.pub.util.param.R
	 * @Author: Ming
	 * @Date: 2021/5/19 20:23
	 */
	@ResponseBody
	@PostMapping("/saveApplicant")
	public R saveApplicant(@RequestBody UserInfoVO userInfoVO, HttpServletRequest request){
		//获取当前登录用户数据
		UserDO userInfo = orderService.getUserInfo(request);
		return orderService.saveApplicant(userInfoVO,userInfo);
	}
	/**
	 * @Description: TODO 根据订单号查询挂靠单位
	 * @param /odrNo 订单号
	 * @return: com.bdzl.pub.util.param.R
	 * @Author: Ming
	 * @Date: 2021/5/19 20:23
	 */
	@ResponseBody
	@PostMapping("/callCompany")
	public R callCompany(@RequestBody Map<String, Object> params){
		String odrNo = (String) params.get("odrNo");
		return R.ok(orderService.callCompany(odrNo));
	}

	/**
	 * @Description: TODO 添加挂靠单位
	 * @param orderCallDO 挂靠单位
	 * @return: com.bdzl.pub.util.param.R
	 * @Author: Ming
	 * @Date: 2021/5/19 20:23
	 */
	@ResponseBody
	@PostMapping("/saveCallCompany")
	public R saveCallCompany(@RequestBody OrderCallDO orderCallDO, HttpServletRequest request){
		//获取当前登录用户数据
		UserDO userDO = orderService.getUserInfo(request);
		if (orderCallDO.getCallComName()==null||orderCallDO.getCallCredate()==null||orderCallDO.getCallLegal()==null||orderCallDO.getCallLegalPhone()==null||orderCallDO.getCallRoadTransport()==null){
			return R.error("请补充挂靠单位信息！");
		}
		if (orderCallDO.getCallId()!=null){
			orderCallService.update(orderCallDO);
		}else {
			orderCallDO.setCallCreuser(userDO.getUserId());
			orderCallDO.setCallCretime(new Date());
			orderCallService.save(orderCallDO);
		}
		return R.ok();

	}
	/**
	 * @Description: TODO 查询实际用车人
	 * @param /custCardId 客户id
	 * @param /odrNo 订单号
	 * @return: com.bdzl.pub.util.param.R
	 * @Author: Ming
	 * @Date: 2021/5/19 20:23
	 */
	@ResponseBody
	@PostMapping("/actualUser")
	public R actualUser(@RequestBody Map<String, Object> params){
		String custCardId = (String) params.get("custCardId");
		String odrNo = (String) params.get("odrNo");
		return R.ok(orderService.actualUser(custCardId,odrNo));
	}

	/**
	 * @Description: TODO 添加实际用车人
	 * @param userInfoVO 客户信息
	 * @return: com.bdzl.pub.util.param.R
	 * @Author: Ming
	 * @Date: 2021/5/19 20:23
	 */
	@ResponseBody
	@PostMapping("/saveActualUser")
	public R saveActualUser(@RequestBody UserInfoVO userInfoVO, HttpServletRequest request){
		//获取当前登录用户数据
		UserDO userDO = orderService.getUserInfo(request);
		return orderService.saveActualUser(userInfoVO,userDO);
	}
	/**
	 * @Description: TODO 查询联系人
	 * @param /relCustUser 申请人id
	 * @param /relOdrNo 订单编号
	 * @param /relType 关系人类型：Guarantee：担保人 Contact：联系人
	 * @return: com.bdzl.pub.util.param.R
	 * @Author: Ming
	 * @Date: 2021/5/19 20:23
	 */
	@ResponseBody
	@PostMapping("/contactPerson")
	public R contactPerson(@RequestBody Map<String, Object> params){
		Integer relCustUser = (Integer) params.get("relCustUser");
		String relOdrNo = (String) params.get("relOdrNo");
		return R.ok(orderService.contactPerson(relOdrNo,relCustUser,"Contact"));
	}
	/**
	 * @Description: TODO 添加联系人
	 * @param orderRelDO 联系人数据
	 * @return: com.bdzl.pub.util.param.R
	 * @Author: Ming
	 * @Date: 2021/5/19 20:23
	 */
	@ResponseBody
	@PostMapping("/saveContactPerson")
	public R saveContactPerson(@RequestBody OrderRelDO[] orderRelDO){
		for (OrderRelDO relDO : orderRelDO) {
			relDO.setRelType("Contact");
			if (relDO.getRelName()==null||relDO.getRelRelation()==null||relDO.getRelPhone()==null){
				return R.error("请补充联系人信息！");
			}
			orderService.saveContactPerson(relDO);
		}
		return R.ok();
	}

	/**
	 * @Description: TODO 删除联系人
	 * @param /relId id
	 * @return: com.bdzl.pub.util.param.R
	 * @Author: Ming
	 * @Date: 2021/5/19 20:23
	 */
	@ResponseBody
	@PostMapping("/removeContactPerson")
	public R removeContactPerson(@RequestBody Map<String, Object> params){
		Integer relId = (Integer) params.get("relId");
		orderRelService.remove(relId);
		return R.ok();
	}
	/**
	 * @Description: TODO 查询担保人
	 * @param /relCustUser 申请人id
	 * @param /relOdrNo 订单编号
	 * @param /relType 关系人类型：Guarantee：担保人 Contact：联系人
	 * @return: com.bdzl.pub.util.param.R
	 * @Author: Ming
	 * @Date: 2021/5/19 20:23
	 */
	@ResponseBody
	@PostMapping("/guarantor")
	public R guarantor(@RequestBody Map<String, Object> params){
		Integer relCustUser = (Integer) params.get("relCustUser");
		String relOdrNo = (String) params.get("relOdrNo");
		return R.ok(orderService.guarantor(relOdrNo,relCustUser));
	}

	/**
	 * @Description: TODO 添加担保人
	 * @param userInfoVOS 担保人信息
	 * @return: com.bdzl.pub.util.param.R
	 * @Author: Ming
	 * @Date: 2021/5/19 20:23
	 */
	@ResponseBody
	@PostMapping("/saveGuarantor")
	public R saveGuarantor(@RequestBody UserInfoVO[] userInfoVOS , HttpServletRequest request){
		//获取当前登录用户数据
		UserDO userDO = orderService.getUserInfo(request);
		for (UserInfoVO userInfoVO : userInfoVOS) {
			if (userInfoVO.getConCardPeople()==null||userInfoVO.getConCardEmblem()==null||userInfoVO.getConName()==null||userInfoVO.getConSex()==null||
			userInfoVO.getConBirth()==null||userInfoVO.getConCardid()==null||userInfoVO.getConCardAdr()==null||userInfoVO.getConMobile()==null
			||userInfoVO.getConMonthIn()==null){
				return R.error("请补充担保人信息！");
			}
			if (userInfoVO.getConMobile()!=null && !"".equals(userInfoVO.getConMobile())){
				//校验手机号
				Object checkPhoneCode = authFeignService.checkPhoneCode(userInfoVO.getConMobile(), userInfoVO.getVerificationCode());
				HashMap result = (HashMap) checkPhoneCode;
				if ("0002".equals(result.get("respCode"))){
				return R.error("手机号："+userInfoVO.getConMobile()+"，验证码错误！");
				}
			}
			if (userInfoVO.getConId()!=null){
				userInfoVO.setConModuser(userDO.getUserId());
				userInfoVO.setConModtime(new Date());
				adminFeignService.updateUserInfo(userInfoVO);
				//获取担保人与申请人相关信息
				OrderRelDO orderRelDO = userInfoVO.getOrderRelDO();
				if (orderRelDO.getRelRelation()==null||orderRelDO.getRelJob()==null){
					return R.error("请补充担保人信息！");
				}
				if (orderRelDO.getRelId()!=null){
					orderRelService.update(orderRelDO);
				}else {
					orderRelDO.setRelType("Guarantee");
					orderRelDO.setRelName(userInfoVO.getConName());
					orderRelDO.setRelPhone(userInfoVO.getConMobile());
					orderRelDO.setRelMonthIn(new BigDecimal(userInfoVO.getConMonthIn()));
					orderRelDO.setRelCardid(String.valueOf(userInfoVO.getConId()));
					orderRelService.save(orderRelDO);
				}
			}else {
				userInfoVO.setConCreuser(userDO.getUserId());
				userInfoVO.setConCretime(new Date());
				Object userInfo = adminFeignService.saveUserInfo(userInfoVO);
				HashMap result = (HashMap) userInfo;
				System.out.println("------------------"+result);
				Integer clientId = (Integer) result.get("data");
				//获取担保人与申请人相关信息
				OrderRelDO orderRelDO = userInfoVO.getOrderRelDO();
				if (orderRelDO.getRelId()!=null){
					orderRelService.update(orderRelDO);
				}else {
					orderRelDO.setRelType("Guarantee");
					orderRelDO.setRelName(userInfoVO.getConName());
					orderRelDO.setRelPhone(userInfoVO.getConMobile());
					orderRelDO.setRelMonthIn(new BigDecimal(userInfoVO.getConMonthIn()));
					orderRelDO.setRelCardid(String.valueOf(clientId));
					orderRelService.save(orderRelDO);
				}
			}
		}
		return R.ok();
	}

	/**
	 * @Description: TODO 删除担保人
	 * @param /relId id
	 * @return: com.bdzl.pub.util.param.R
	 * @Author: Ming
	 * @Date: 2021/5/19 20:23
	 */
	@ResponseBody
	@PostMapping("/removeGuarantor")
	public R removeGuarantor(@RequestBody Map<String, Object> params){
		Integer relId = (Integer) params.get("relId");
		//获取担保人数据
		Integer orderRelByRelCardid = orderRelService.getOrderRelByRelCardid(relId);
		//删除担保人信息
		adminFeignService.removeUserInfo(relId);
		orderRelService.remove(orderRelByRelCardid);
		return R.ok();
	}


	/**
	* @Description: TODO 计算公式
	* @param order 申请单
	* @return: com.bdzl.pub.util.param.R
	* @Author: Ming
	* @Date: 2021/5/22 11:07
	*/
	@PostMapping("/calculationFormula")
	@ResponseBody
	public R calculationFormula(@RequestBody OrderVO order){
		return orderService.calculationFormula(order);
	}

	/**
	* @Description: TODO 复审数据
	* @param /odrNo 订单编号
	* @return: com.bdzl.pub.util.param.R
	* @Author: Ming
	* @Date: 2021/5/25 10:24
	*/
	@PostMapping("/review")
	@ResponseBody
	public R review(@RequestBody Map<String, Object> params){
		String odrNo = (String) params.get("odrNo");
		return R.ok(orderService.review(odrNo));
	}
	/**
	 * @Description: TODO 家访图片上传
	 * @param /url 图片、视频
	 * @param /type 类型 img :图片 video :视频 7：资方合同 8：公司合同  9：确认书  10：协议书
	 * @return: com.bdzl.pub.util.param.R
	 * @Author: Ming
	 * @Date: 2021/5/25 10:24
	 */
	@PostMapping("/saveHomeVisitImg")
	@ResponseBody
	public String saveHomeVisitImg(@RequestBody Map<String, Object> params) throws IOException {
		String url = (String) params.get("url");
		String type = (String) params.get("type");
		String docUrl = null;
		if ("img".equals(type)){//家访图片
			MultipartFile file = BASE64DecodedMultipartFile.base64ToMultipart(url);
			docUrl = OssUploadFileUtil.RandomFileName.uploadToOSS(file, "loan/home_visit/img");
		}else if ("video".equals(type)){//家访视频
			MultipartFile file = BASE64DecodedMultipartFile.base64ToMultipart(url);
			docUrl = OssUploadFileUtil.RandomFileName.uploadToOSS(file, "loan/home_visit/video");
		}else if ("7".equals(type)){//资方合同
			MultipartFile file = BASE64DecodedMultipartFile.base64ToMultipart(url);
			docUrl = OssUploadFileUtil.RandomFileName.uploadToOSS(file, "loan/contract/capital");
		}else if ("8".equals(type)){//公司合同
			MultipartFile file = BASE64DecodedMultipartFile.base64ToMultipart(url);
			docUrl = OssUploadFileUtil.RandomFileName.uploadToOSS(file, "loan/contract/company");
		}else if ("9".equals(type)){//确认书
			MultipartFile file = BASE64DecodedMultipartFile.base64ToMultipart(url);
			docUrl = OssUploadFileUtil.RandomFileName.uploadToOSS(file, "loan/contract/confirmation");
		}else if ("10".equals(type)){//协议书
			MultipartFile file = BASE64DecodedMultipartFile.base64ToMultipart(url);
			docUrl = OssUploadFileUtil.RandomFileName.uploadToOSS(file, "loan/contract/agreement");
		}else if ("13".equals(type)){//发票信息
			MultipartFile file = BASE64DecodedMultipartFile.base64ToMultipart(url);
			docUrl = OssUploadFileUtil.RandomFileName.uploadToOSS(file, "loan/contract/invoice");
		}else if ("14".equals(type)){//保单信息
			MultipartFile file = BASE64DecodedMultipartFile.base64ToMultipart(url);
			docUrl = OssUploadFileUtil.RandomFileName.uploadToOSS(file, "loan/contract/guaranteeSlip");
		}else if ("15".equals(type)){//合格证信息
			MultipartFile file = BASE64DecodedMultipartFile.base64ToMultipart(url);
			docUrl = OssUploadFileUtil.RandomFileName.uploadToOSS(file, "loan/contract/qualified");
		}else if ("16".equals(type)){//车辆登记信息
			MultipartFile file = BASE64DecodedMultipartFile.base64ToMultipart(url);
			docUrl = OssUploadFileUtil.RandomFileName.uploadToOSS(file, "loan/contract/car");
		}
		return docUrl;
	}
	/**
	 * @Description: TODO 家访保存
	 * @param orderVO 订单数据
	 * @return: com.bdzl.pub.util.param.R
	 * @Author: Ming
	 * @Date: 2021/5/25 10:24
	 */
	@PostMapping("/saveHomeVisit")
	@ResponseBody
	@Transactional
	public R saveHomeVisit(@RequestBody OrderVO orderVO, HttpServletRequest request){
		//获取当前登录用户数据
		UserDO userDO = orderService.getUserInfo(request);
		//更新订单数据
		orderService.update(orderVO);
		//获取家访信息
		OrderVisitorVO orderVisitorVO = orderVO.getOrderVisitorVO();
		if (orderVO.getOdrState()!=null){
			HashMap<String,Object> map = new HashMap<>();
			map.put("odrNo",orderVO.getOdrNo());
			map.put("statVal",orderVO.getOdrState());
			this.check(map);
		}
		if (orderVisitorVO!=null){
			if (orderVisitorVO.getVisitorId()!=null){
				orderVisitorService.update(orderVisitorVO);
			}
			//家访图片集合
			List<OrderVisitImgDO> orderVisitDOListImg = orderVisitorVO.getOrderVisitDOListImg();
			//家访视频
			OrderVisitImgDO orderVisitDOVideo = orderVisitorVO.getOrderVisitDOVideo();

			if (orderVisitDOListImg != null && orderVisitDOListImg.size()>0){
				for (OrderVisitImgDO orderVisitImgDO : orderVisitDOListImg) {
					if (orderVisitImgDO.getVimgId()!=null){
						orderVisitImgService.update(orderVisitImgDO);
					}else {
						orderVisitImgDO.setVimgOdr(orderVO.getOdrNo());
						orderVisitImgDO.setVimgCreuser(userDO.getUserId());
						orderVisitImgDO.setVimgTime(new Date());
						orderVisitImgService.save(orderVisitImgDO);
					}
				}
			}
			if (orderVisitDOVideo!=null){
				if (orderVisitDOVideo.getVimgId()!=null){
					orderVisitImgService.update(orderVisitDOVideo);
				}else {
					orderVisitDOVideo.setVimgOdr(orderVO.getOdrNo());
					orderVisitDOVideo.setVimgCreuser(userDO.getUserId());
					orderVisitDOVideo.setVimgTime(new Date());
					orderVisitImgService.save(orderVisitDOVideo);
				}
			}
		}
		return R.ok();
	}

	/**
	 * @Description: TODO 签署合同保存
	 * @param orderVO 订单数据
	 * @return: com.bdzl.pub.util.param.R
	 * @Author: Ming
	 * @Date: 2021/5/25 10:24
	 */
	@PostMapping("/saveContract")
	@ResponseBody
	@Transactional
	public R saveContract(@RequestBody OrderVO orderVO, HttpServletRequest request){
		//获取当前登录用户数据
		UserDO userDO = orderService.getUserInfo(request);

		//上传完的合同id
		Integer id = null;
		//获取合同信息
		List<OdrFileCategoryVO> odrFileCategoryVOList = orderVO.getOdrFileCategoryVOList();
		if (odrFileCategoryVOList!=null && odrFileCategoryVOList.size()>0){
			for (OdrFileCategoryVO odrFileCategoryVO : odrFileCategoryVOList) {
				List<OdrFileInfoVO> odrFileInfoDOList = odrFileCategoryVO.getOdrFileInfoDOList();
				if (odrFileInfoDOList!=null&&odrFileInfoDOList.size()>0){
					for (OdrFileInfoDO odrFileInfoDO : odrFileInfoDOList) {
						if (odrFileInfoDO.getOfiId()!=null){
							odrFileInfoService.update(odrFileInfoDO);
							id = odrFileInfoDO.getOfiId();
						}else {
							odrFileInfoDO.setOfiCreuser(userDO.getUserId());
							odrFileInfoDO.setOfiCretime(new Date());
							odrFileInfoService.save(odrFileInfoDO);
							id = odrFileInfoDO.getOfiId();
						}
					}
				}
			}
		}
		OrderDO orderDO = orderService.get(orderVO.getOdrNo());
		if (orderVO.getOdrState()!=null){
			HashMap<String,Object> map = new HashMap<>();
			map.put("odrNo",orderVO.getOdrNo());
			map.put("statVal",orderVO.getOdrState());
			map.put("nowStatVal",orderDO.getOdrState());
			this.check(map);
		}
		//更新订单数据
		orderService.update(orderVO);
		return R.ok(id);
	}

	/**
	 * @Description: TODO 删除合同
	 * @param //id  合同id
	 * @return: com.bdzl.pub.util.param.R
	 * @Author: Ming
	 * @Date: 2021/5/25 10:24
	 */
	@PostMapping("/removeContract")
	@ResponseBody
	@Transactional
	public R removeContract(@RequestBody Map<String, Object> params){
		//删除合同
		Integer id = (Integer) params.get("id");
		OdrFileInfoDO odrFileInfoDO = odrFileInfoService.get(id);
		//删除阿里云图片
		OssUploadFileUtil.remove(odrFileInfoDO.getOfiUrl());
		if (odrFileInfoService.remove(id)>0){
			return R.ok();
		}
		return R.error();
	}

	/**
	 * 下载协议书
	 * @return
	 * , PropertiesUtils.getKey("tem_type")
	 */
	@PostMapping("/downloadAgreement")
	@ResponseBody
	public void downloadAgreement(String odrNo, HttpServletResponse response, HttpServletRequest request,String temType) throws IOException {
		//type  下载文件类型  1：word  2：pdf
		orderService.downloadAgreement(odrNo,response,request,temType);
	}


	/**
	 * @Author Zhn
	 * @Description //TODO 回款确认列表
	 * @Date 15:37 2021/5/28
	 * @Param
	 * @return
	 **/
	@PostMapping("/payment")
	@ResponseBody
	public List<PaymentVO> payment(@RequestBody Map<String,Object> map){
		Query query = new Query(map);
		//提取主单的部分字段
		List<PaymentVO> paymentVOS = orderService.paymentList(query);
		for (PaymentVO paymentVO:paymentVOS) {
			//获取客户
			UserInfoVO userInfo = adminFeignService.getUserInfo(paymentVO.getOdrCust(), paymentVO.getOdrNo());
			paymentVO.setConName(userInfo.getConName());
			//获取打款列表
			List<OrderPayRecDO> orderPayRecDOS = orderPayRecService.listByOprOdr(paymentVO.getOdrNo());
			String oprPayItem = "";
			for (OrderPayRecDO item:orderPayRecDOS) {
				if (item.getOprPayItem().contains("carpay")){
					//打款摘要
					paymentVO.setOprPayExplain(item.getOprPayExplain());
					//打款时间
					paymentVO.setOprTime(item.getOprTime());
				}
				if (paymentVO.getOprTime()==null){
					//打款时间
					paymentVO.setOprTime(item.getOprTime());
				}
				oprPayItem = oprPayItem + item.getOprPayItem();
			}
			paymentVO.setOprPayItem(oprPayItem);
			//查询打款关系表
			List<OrderAccountDO> orderAccountDOS = orderAccountService.listByOdrNo(paymentVO.getOdrNo());
			//查询打款账户信息
			OrderPayAccountDO orderPayAccountDO = orderPayAccountService.get(orderAccountDOS.get(0).getPayId());
			//户名
			paymentVO.setPayName(orderPayAccountDO.getPayName());
			//查询车辆信息
			OrderVehDO byVehOdr = orderVehService.getByVehOdr(paymentVO.getOdrNo());
			paymentVO.setVehBrand(byVehOdr.getVehBrand());//品牌
			paymentVO.setVehModel(byVehOdr.getVehModel());//车型
		}
		return paymentVOS;
	}

	/**
	 * @Author Zhn
	 * @Description //TODO 回款确认列表
	 * @Date 15:37 2021/5/28
	 * @Param
	 * @return
	 **/
	@PostMapping("/confirm")
	@ResponseBody
	public R confirm(@RequestBody OrderDO orderDO){
		orderDO.setOdrReturnDate(new Date());
		orderDO.setOdrReturnStatus(1);
		orderService.update(orderDO);
		return R.ok();
	}

	/**
	 * @Author Zhn
	 * @Description //TODO 日回款打款金额统计
	 * @Date 16:03 2021/5/29
	 * @Param [map] 日期
	 * @return com.bdzl.pub.util.param.R
	 **/
	@PostMapping("/moneyDay")
	@ResponseBody
	public R moneyDay(@RequestBody Map<String,Object> map){
		R r = R.ok();
		if (map.get("timeDay").equals("")){
			map = new HashMap<>();
			map.put("timeDay",DateUtils.format(new Date()));
		}
		r.put("todayOdrLoanSys",orderService.sumOdrLoanSys(map));
		r.put("todayOdrPayMoney",orderService.sumOdrPayMoney(map));
		return r;
	}

	/**
	 * @Author Zhn
	 * @Description //TODO 月回款打款金额统计
	 * @Date 15:37 2021/5/28
	 * @Param [map] 月份
	 * @return
	 **/
	@PostMapping("/moneyMonth")
	@ResponseBody
	public R moneyMonth(@RequestBody Map<String,Object> map){
		R r = R.ok();
		if (map.get("timeMonth").equals("")){
			map = new HashMap<>();
			map.put("timeMonth",DateUtils.format(new Date(),"yyyy-MM"));
		}
		r.put("monthOdrLoanSys",orderService.sumOdrLoanSys(map));
		r.put("monthOdrPayMoney",orderService.sumOdrPayMoney(map));
		return r;
	}

	/**
	 * 订单统计 list
	 */
	@ResponseBody
	@GetMapping("/statisticsOrderList")
	public PageUtils statisticsOrderList(@RequestParam Map<String, Object> params){
		Query query = new Query(params);
		List<OrderVO> orderVOList = orderService.statisticsOrderList(query);
		for (OrderVO orderVO:orderVOList) {
			List<String> list = new ArrayList();
			for (ActOdrStatusDO actOdrStatusDO:orderVO.getActOdrStatusDOList()) {//actOdrStatusDOList
				if(actOdrStatusDO.getStatName()!=null){
					list.add(actOdrStatusDO.getStatName());
				}
			}
			String name = StringUtils.getStringByList(list);//拼接状态妹子
			orderVO.setStateName(name);
		}
		Integer count = orderService.statisticsOrderListcount(query);
		PageUtils pageUtils = new PageUtils(orderVOList,count);
		return pageUtils;
	}




	/**
	 * @Description: TODO 邮寄合同、上传凭证保存
	 * @param orderVO 订单数据
	 * @return: com.bdzl.pub.util.param.R
	 * @Author: Ming
	 * @Date: 2021/5/25 10:24
	 */
	@PostMapping("/save邮寄合同")
	@ResponseBody
	@Transactional
	public R save邮寄合同(@RequestBody OrderVO orderVO, HttpServletRequest request){
		//获取当前登录用户数据
		UserDO userDO = orderService.getUserInfo(request);

		//上传完的合同id
		Integer id = null;
		//获取合同信息
		List<OdrFileCategoryVO> odrFileCategoryVOList = orderVO.getOdrFileCategoryVOList();
		if (odrFileCategoryVOList!=null && odrFileCategoryVOList.size()>0){
			for (OdrFileCategoryVO odrFileCategoryVO : odrFileCategoryVOList) {
				List<OdrFileInfoVO> odrFileInfoDOList = odrFileCategoryVO.getOdrFileInfoDOList();
				if (odrFileInfoDOList!=null&&odrFileInfoDOList.size()>0){
					for (OdrFileInfoDO odrFileInfoDO : odrFileInfoDOList) {
						if (odrFileInfoDO.getOfiId()!=null){
							odrFileInfoService.update(odrFileInfoDO);
							id = odrFileInfoDO.getOfiId();
						}else {
							odrFileInfoDO.setOfiCreuser(userDO.getUserId());
							odrFileInfoDO.setOfiCretime(new Date());
							odrFileInfoService.save(odrFileInfoDO);
							id = odrFileInfoDO.getOfiId();
						}
					}
				}
			}
		}
		OrderDO orderDO = orderService.get(orderVO.getOdrNo());
		if (orderVO.getOdrState()!=null){
			HashMap<String,Object> map = new HashMap<>();
			map.put("odrNo",orderVO.getOdrNo());
			map.put("statVal",orderVO.getOdrState());
			map.put("nowStatVal",orderDO.getOdrState());
			this.check(map);
		}
		//更新订单数据
		orderService.update(orderVO);
		return R.ok(id);
	}
}
